package com.atguigu.jxc.service.impl;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.bejson.pojo.Attributes;
import com.atguigu.jxc.entity.bejson.pojo.Children;
import com.atguigu.jxc.entity.bejson.pojo.JsonRootBean;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    GoodsTypeDao goodsTypeDao;

    @Override
    public List<JsonRootBean> loadGoodsType() {
        List<GoodsType> list = goodsTypeDao.list();
        //承载父类别数据
        JsonRootBean father = new JsonRootBean();
        //每一个 children 承载 2级分类
        //承载 所有 2级分类
        List<Children> childrenList = new ArrayList<>();
        for (GoodsType g:list
        ) {
            //一级分类
            if(g.getPId() == -1){
                father.setId(g.getGoodsTypeId());
                father.setText(g.getGoodsTypeName());
                Attributes attributes = new Attributes();
                attributes.setState(g.getGoodsTypeState());
                father.setAttributes(attributes);
                father.setState("close");
                father.setIconCls("goods-type");
                //这里是最大的 父节点 remove掉

            }
        }
        for (GoodsType g:list
        ) {
            //二级分类
            if(g.getPId()==1){
                Children children = new Children();
                children.setId(g.getGoodsTypeId());
                children.setText(g.getGoodsTypeName());
                Attributes attributes = new Attributes();
                attributes.setState(g.getGoodsTypeState());
                children.setAttributes(attributes);
                children.setState("close");
                children.setIconCls("goods-type");
                childrenList.add(children);
            }
        }
        for (int i = 0; i < childrenList.size(); i++) {
            //二级类别 对象
            Children children1 = childrenList.get(i);
            //承载所有的三级分类
            List<Children>children3s = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                //如果	二级类别对象的 ID == 从list中取出对象的 PID
                GoodsType goodsType = list.get(j);
                //从这里判定出的 则为二级分类和二级分类对应的 三级分类
                if(children1.getId()== goodsType.getPId()){
                    Children children3 = new Children();
                    children3.setId(goodsType.getGoodsTypeId());
                    children3.setText(goodsType.getGoodsTypeName());
                    Attributes attributes = new Attributes();
                    attributes.setState(goodsType.getGoodsTypeState());
                    children3.setAttributes(attributes);
                    children3.setState("close");
                    children3.setIconCls("goods-type");
                    children3s.add(children3);
                    children1.setChildren(children3s);
                }
            }
        }
        father.setChildren(childrenList);
        List<JsonRootBean> list1 = new ArrayList<>();
        list1.add(father);
        System.out.println(father);
        return list1;
    }
}
